package com.df.bpm.utils.businessViewerV2.entity;

import com.df.bpm.utils.businessViewerV2.Main;
import com.df.bpm.utils.businessViewerV2.entityType.C_entityType;
import com.df.bpm.utils.businessViewerV2.entityType.M_entityType;
import com.df.common.utils.CommonUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;

public class C_entity {

    static Db_entity database=new Db_entity();

    static void editEntityName(JTable iTable, int row, List<M_entityDto> list) {
        String oldValue=""+iTable.getValueAt(row, 2);
        String tips="请输入类型名称：";
        String newValue= (String) JOptionPane.showInputDialog(null, tips, "实体名称", JOptionPane.PLAIN_MESSAGE, null, null, oldValue);
        if(newValue!=null && !newValue.isEmpty() && !oldValue.equals(newValue)){
            iTable.setValueAt(newValue, row, 2);
            M_entity temp = list.get(row)
                    .setName(newValue);
            if(newValue.equals("-")){
                if(CommonUtils.varIsNotBlank(temp.getBusinessEntityIndex())){
                    if(database.delete(temp.getBusinessEntityIndex())>0){
                        list.remove(row);
                        DefaultTableModel model=(DefaultTableModel)(iTable.getModel());
                        model.removeRow(row);
                    }
                }
            }else {
                database.update(temp);
            }
        }
    }

    public static void changeType(JTable iTable, int row, List<M_entityDto> list) throws Exception {
        M_entityType newType= C_entityType.selectOne();
        if(CommonUtils.varIsNotBlank(newType) && !newType.getEntityTypeIndex().equals(list.get(row).getEntityTypeIndex())){
            iTable.setValueAt(newType.getEntityTypeIndex(), row, 3);
            iTable.setValueAt(newType.getName(), row, 4);
            M_entity temp = list.get(row);
            temp.setEntityTypeIndex(newType.getEntityTypeIndex());
            database.update(temp);
        }
    }

    public static List<M_entityDto> search(List<JTextField> fields) {
        M_entityDto temp=new M_entityDto();
        if (CommonUtils.varIsNotBlank(fields.get(0).getText())) {
            temp.setName(fields.get(0).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(1).getText())) {
            temp.setTypeName(fields.get(1).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(2).getText())) {
            temp.setMasterBusiness(fields.get(2).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(3).getText())) {
            temp.setOutput4Business(fields.get(3).getText());
        }
        if (CommonUtils.varIsNotBlank(fields.get(4).getText())) {
            temp.setInput2Business(fields.get(4).getText());
        }
        return database.getList4Like(temp);
    }

    public static M_entityDto selectOne() {
        V_entity vEntity=new V_entity();
        vEntity.repaint();
        vEntity.setVisible(true);

        return vEntity.selectResult;
    }

    public static void main(String[] args) throws Exception {
        Main.getScreenMaxSize();
        String strEntity=selectOne().toString();
        System.out.println(strEntity);
    }
}
